package com.chk.user.controller.visiter;



import com.chk.commom.constants.AppHttpCodeEnum;
import com.chk.commom.utils.CommonResult;
import com.chk.user.entity.User;
import com.chk.user.service.UserService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-10-20 11:00:17
 */

@RefreshScope // 获取配置中心   nacos中的配置
@RestController
@RequestMapping("user")
public class UserController{
//    @Value("${id}")
//    private String id;
    @Autowired
    private UserService userService;
    @GetMapping("/{userId}")
    User getUserById(@PathVariable(value = "userId")Integer userId){
        return userService.getById(userId);
    }
    @GetMapping("/code")
    public CommonResult code(HttpServletRequest request,
                     HttpServletResponse res) throws IOException {
       return userService.getCode(request,res);
    }
    @PostMapping("/login")
    public CommonResult login(@RequestBody User user, HttpServletRequest request){
        return userService.login(user,request);
    }
    @PostMapping("/logout")
    public CommonResult loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return CommonResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

